package com.zhang.controller;

import com.zhang.dao.DepartmentDao;
import com.zhang.dao.EmployeeDao;
import com.zhang.pojo.Department;
import com.zhang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * @author ZhangJunqi
 * @Date 2022/2/23 -20:15
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;
    @Resource
    private DepartmentDao departmentDao;

    @GetMapping(value = "/getAllEmployee")
    public String getAllEmployee(Model model){
        Collection<Employee> employeeList=employeeDao.getAllEmployee ();
        model.addAttribute ("employeeList",employeeList);
        return "emp/list";
    }

    @GetMapping(value = "/emp")
    public String addEmpPage(Model model){
        Collection<Department> departmentList=departmentDao.getAllDepartment ();
        model.addAttribute ("departmentList",departmentList);
        return "emp/addEmp";
    }

    @PostMapping(value = "/emp")
    public String addEmp(Employee employee){
        System.out.println(employee);
        employeeDao.saveEmployee (employee);
        return "redirect:/getAllEmployee";
    }

    @GetMapping(value = "/empUpdate/{id}")
    public String updateEmpPage(@PathVariable("id") Integer id,Model model){
        Employee employee=employeeDao.getEmployeeById (id);
        System.out.println(employee.toString ());
        model.addAttribute ("employee",employee);
        Collection<Department> departmentList=departmentDao.getAllDepartment ();
        model.addAttribute ("departmentList",departmentList);
        return "emp/updateEmp";
    }
    @GetMapping(value = "/empDelete/{id}")
    public String empDelete(@PathVariable("id") Integer id){
        System.out.println(id);
        employeeDao.deleteEmployeeById (id);
        return "redirect:/getAllEmployee";
    }

    @PostMapping(value = "/empUpdate")
    public String empUpdate(Employee employee){
        System.out.println(employee);
        employeeDao.saveEmployee (employee);
        return "redirect:/getAllEmployee";
    }





}
